package com.tedu.ivos.user.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.tedu.ivos.base.exception.ServiceException;
import com.tedu.ivos.base.response.JsonResult;
import com.tedu.ivos.base.response.StatusCode;
import com.tedu.ivos.user.pojo.dto.UserLoginParam;
import com.tedu.ivos.user.pojo.dto.UserQuery;
import com.tedu.ivos.user.pojo.dto.UserSaveParam;
import com.tedu.ivos.user.pojo.vo.UserVO;
import com.tedu.ivos.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@Api(tags = "用户模块")
@RequestMapping("/v1/user/")
public class UserController {

    @Autowired
    UserService userService;

    @ApiOperation("1.用户登录")
    @ApiOperationSupport(order = 10)
    @PostMapping("login")
    public JsonResult login(@RequestBody UserLoginParam userLoginParam) {
        log.debug("用户登录:userLoginParam={}" , userLoginParam);
        UserVO userVO = userService.login(userLoginParam);
        if (userVO == null) {
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if (!userVO.getPassword().equals(userLoginParam.getPassword())) {
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }

        return JsonResult.ok(userVO);
    }

    @ApiOperation("2.用户注册/更新")
    @ApiOperationSupport(order = 20)
    @PostMapping("save")
    public JsonResult saveUser(@RequestBody UserSaveParam userSaveParam) {
        log.debug("保存用户:{}" , userSaveParam);
        userService.saveUser(userSaveParam);
        return JsonResult.ok();
    }

    @ApiOperation("3.用户查询")
    @ApiOperationSupport(order = 30)
    @GetMapping("select")
    public JsonResult selectUser(UserQuery userQuery){
        log.debug("查询用户:userQuery={}" , userQuery);
        List<UserVO> list = userService.selectUser(userQuery);

        return JsonResult.ok(list);
    }

    @ApiOperation("4.重置密码")
    @ApiOperationSupport(order = 40)
    @PostMapping("update/password/{userId}")
    public JsonResult resetPassword(@PathVariable Long userId) {
        log.debug("修改密码:{}" , userId);
        userService.updatePassword(userId);

        return JsonResult.ok();
    }

    @ApiOperation("5.修改员工状态")
    @ApiOperationSupport(order = 50)
    @PostMapping("update/status/{userId}/{status}")
    public JsonResult updateStatus(@PathVariable Long userId,
                                   @PathVariable String status) {
        log.debug("修改员工状态:userId={},status={}" , userId, status);
        userService.updateStatus(userId, status);

        return JsonResult.ok();
    }

    @ApiOperation("6.删除员工")
    @ApiOperationSupport(order = 60)
    @PostMapping("delete/{userId}")
    public JsonResult deleteUser(@PathVariable Long userId) {
        log.debug("删除员工:userId={}" , userId);
        userService.deleteUser(userId);

        return JsonResult.ok();
    }

    @ApiOperation("7.查询上级领导")
    @ApiOperationSupport(order = 70)
    @GetMapping("select/audit/{parentId}")
    public JsonResult selectAuditList(@PathVariable Long parentId) {
        log.debug("查询审批人列表：{}" , parentId);
        List<UserVO> list = userService.selectAuditList(parentId);

        return JsonResult.ok(list);
    }
}
